package sunwin.yois.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import sunwin.yois.web.common.R;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsPage;
import sunwin.yoms.comm.model.NameAndValueModel;
import sunwin.yoms.domain.publicty.VideoDomain;
import sunwin.yoms.service.publicty.VideoService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mt on 2015-9-6.
 */
@Controller
public class VideoController {
    @Autowired
    private VideoService videoService;

    /**
     * 访问视频首页
     *
     * @param modelMap
     * @param pix      指定页数
     * @param ot       指定哪种排序，1.按时间，2.按热度
     */
    @RequestMapping(value = R.VideoIndex, name = "video/", method = RequestMethod.GET)
    public void videoIndex(ModelMap modelMap,
                           @RequestParam(required = false, defaultValue = "1") int pix,
                           @RequestParam(required = false, defaultValue = "1") int ot) {
        modelMap.addAttribute("ot", ot);

        getVideos(modelMap, pix, ot);
    }

    /**
     * 访问视频播放页面
     * @param modelMap
     * @param vid
     */
    @RequestMapping(value = R.VideoDetailed, name = "video/", method = RequestMethod.GET)
    public void videoDetailed(ModelMap modelMap, int vid) throws YomsException {
        VideoDomain videoDomain = videoService.getVideoById(vid);

        if (videoDomain != null) {
            String videoName = videoDomain.getVideoFileName();

            modelMap.addAttribute("video", videoDomain);
            modelMap.addAttribute("videoPath", R.getYomUrl() + "video/" + videoName);
            videoService.updateHot(vid);
        }
    }

    /**
     * 获取不同排序的视频
     *
     * @param modelmap
     * @param pix      指定页数
     * @param ot       指定哪种排序，1.按时间，2.按热度
     */
    private void getVideos(ModelMap modelmap, int pix, int ot) {
        YomsPage yomsPage = new YomsPage();
        yomsPage.setPageIndex(pix);

        List<VideoDomain> domains = null;

        if (ot == 1) {
            domains = videoService.getVideoOrderByTime(yomsPage);
        } else if (ot == 2) {
            domains = videoService.getVideoOrderByHot(yomsPage);
        }

        List<NameAndValueModel> pages = new ArrayList<>(0);
        for (int i = 0; i < yomsPage.getPageCount(); i++) {
            NameAndValueModel mavm = new NameAndValueModel();
            mavm.setValue(String.format("/%s?&pix=%s&ot=%s", R.getVideoDetailed(), i + 1, ot));
            mavm.setName(i + 1 + "");
            pages.add(mavm);
            if (i == 9) {
                break;
            }
        }

        yomsPage.setPageIndex(1);
        yomsPage.setPageSize(9);
        List<VideoDomain> hotVideos = videoService.getVideoOrderByHot(yomsPage);

        modelmap.addAttribute("videos", domains);
        modelmap.addAttribute("hotVideos", hotVideos);
        modelmap.addAttribute("pix", pix);
        modelmap.addAttribute("pages", pages);
        modelmap.addAttribute("pol", yomsPage.getPageCount());
    }
}
